package com.family.gmall.cart.controller;

import com.family.gmall.cart.service.CartService;
import com.family.gmall.common.result.Result;
import com.family.gmall.common.util.AuthContextHolder;
import com.family.gmall.model.order.CartInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Auther: family
 * @Date: 2023/7/27 17:03
 * @Description:
 */
@Api(tags = "购物车控制器")
@RestController
@RequestMapping("/api/cart")
public class CartApiController {

    @Resource
    private CartService cartService;

    @ApiOperation(" 添加购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "skuId", value = "skuId", required = true),
            @ApiImplicitParam(name = "skuNum", value = "skuNum", required = true),
    })
    @PostMapping("addToCart/{skuId}/{skuNum}")
    public Result addToCart(@PathVariable("skuId") Long skuId,
                            @PathVariable("skuNum") Integer skuNum,
                            HttpServletRequest request) {
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)) {
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartService.addToCart(skuId, userId, skuNum);
        return Result.ok();
    }


    @ApiOperation("查询购物车")
    @GetMapping("cartList")
    public Result cartList(HttpServletRequest request) {
        String userId = AuthContextHolder.getUserId(request);
        String userTempId = AuthContextHolder.getUserTempId(request);
        List<CartInfo> cartList = cartService.getCartList(userId, userTempId);
        return Result.ok(cartList);
    }

    @ApiOperation("更新选中状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "skuId", value = "skuId", required = true),
            @ApiImplicitParam(name = "isChecked", value = "isChecked", required = true)
    })
    @GetMapping("checkCart/{skuId}/{isChecked}")
    public Result checkCart(
            @PathVariable(value = "skuId") Long skuId,
            @PathVariable(value = "isChecked") Integer isChecked,
            HttpServletRequest request) {

        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)) {
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartService.checkCart(userId, isChecked, skuId);
        return Result.ok();
    }

    @ApiOperation("删除")
    @ApiImplicitParam(name = "skuId", value = "skuId", required = true)
    @DeleteMapping("deleteCart/{skuId}")
    public Result deleteCart(
            @PathVariable("skuId") Long skuId,
            HttpServletRequest request) {
        // 如何获取userId
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)) {
            // 获取临时用户Id
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartService.deleteCart(skuId, userId);
        return Result.ok();
    }

    @ApiOperation("根据用户Id 获取购物车中选中的商品列表")
    @ApiImplicitParam(name = "userId", value = "userId", required = true)
    @GetMapping("getCartCheckedList/{userId}")
    public List<CartInfo> getCartCheckedList(@PathVariable(value = "userId") String userId) {
        return cartService.getCartCheckedList(userId);
    }

    @ApiImplicitParam(name = "userId", value = "userId", required = true)
    @GetMapping("loadCartCache/{userId}")
    public Result loadCartCache(@PathVariable("userId") String userId) {
        cartService.loadCartCache(userId);
        return Result.ok();
    }
}
